{extend name="iframe" /}
{block name="body"}
<div>
	<ul class="layui-nav" lay-filter="">
		<li class="layui-nav-item"><a onclick="clickInfo()">基本信息</a></li>
		<li class="layui-nav-item"><a onclick="clickResume()">履历</a></li>
		<li class="layui-nav-item layui-this"><a onclick="clickReward()">奖惩信息</a></li>
	</ul>
</div>
<div class="layui-card">
	<div class="layui-card-body">
		<input type="hidden" id="id" name="id" value="{$id|default=''}" />
		<input type="hidden" id="kid" name="kid" value="{$kid|default=''}" />
		<div>
			教师姓名：{$name|default=''}
		</div>
		<a onclick="resumeAdd()" class="layui-btn"><i class="fa fa-plus-circle"></i> 添加行</a>
		<div id="resumeInfo" class="layui-form">
		</div>
	</div>
	<div class="layui-card-body">
		<div class="layui-inline">
			<a onclick="resumeSave()" class="layui-btn"><i class="fa fa-plus-circle"></i> 保存</a>
		</div>
	</div>
</div>
{/block}
{block name="js"}
<script>
	function clickInfo() {
		var id = $('#id').val();
		var kid = $('#kid').val();
		location.href = '/admin/scholar/edit?id=' + id + '&kid=' + kid
	}

	function clickResume() {
		var id = $('#id').val();
		var kid = $('#kid').val();
		location.href = '/admin/scholar/resumeDetail?id=' + id + '&kid=' + kid
	}

	function clickReward() {
		var id = $('#id').val();
		var kid = $('#kid').val();
		location.href = '/admin/scholar/toRewardAndPunish?id=' + id + '&kid=' + kid
	}

	// 初始化内容
	$(function() {
		initResumeInfo();
	});

	function initResumeInfo() {
		var kid = $('#kid').val();
		$.ajax({
			url: '/admin/scholar/rewardAndPunish?kid=' + kid,
			type: 'POST',
			dataType: 'json',
			success: function(result) {
				if (result.code === 1) {
					var resumeInfos = result.data
					if (resumeInfos.length <= 0) {
						return
					}
					var info = '';
					for (let i = 0; i < resumeInfos.length; i++) {
						info+= '<div>\n' +
								'<div class="layui-inline">\n' +
								'<label class="layui-form-label">年份</label>\n' +
								'</div>\n' +
								'<div class="layui-inline">\n' +
								'<input type="text" id="year" name="year" value='+ resumeInfos[i].year +' maxlength="4" class="layui-input" />\n' +
								'</div>\n' +
								'<div class="layui-inline">\n' +
								'<label class="layui-form-label">奖惩信息</label>\n' +
								'</div>\n' +
								'<div class="layui-inline">\n' +
								'<input type="text" id="resume" name="resume" value='+ resumeInfos[i].resume +' class="layui-input" />\n' +
								'</div>\n' +
								'<div class="layui-inline">\n' +
								'<a onclick="resumeDel(this)" class="layui-btn"><i class="fa fa-plus-circle"></i> 删除行</a>\n' +
								'</div>\n' +
								'</div>'
					}
					var div = document.getElementById("resumeInfo");
					div.innerHTML = info;
				}
			}
		});
	}

	function resumeAdd() {
		var div = document.getElementById("resumeInfo");
		var html = document.getElementById("resumeInfo").innerHTML;

		var info = '<div>\n' +
				'<div class="layui-inline">\n' +
				'<label class="layui-form-label">年份</label>\n' +
				'</div>\n' +
				'<div class="layui-inline">\n' +
				'<input type="text" id="year" name="year" value="" maxlength="4" class="layui-input" />\n' +
				'</div>\n' +
				'<div class="layui-inline">\n' +
				'<label class="layui-form-label">奖惩信息</label>\n' +
				'</div>\n' +
				'<div class="layui-inline">\n' +
				'<input type="text" id="resume" name="resume" value="" class="layui-input" />\n' +
				'</div>\n' +
				'<div class="layui-inline">\n' +
				'<a onclick="resumeDel(this)" class="layui-btn"><i class="fa fa-plus-circle"></i> 删除行</a>\n' +
				'</div>\n' +
				'</div>';
		div.innerHTML = html + info;
	}

	function resumeDel(e) {
		$(e).parent().parent().remove();
	}

	function resumeSave() {
		var years = [];
		var isEmptyYear = false;
		$('.layui-form input[id="year"]').each(function (i) {
			if ($(this).val() === undefined || $(this).val() == '') {
				isEmptyYear = true;
			}
			years[i] = $(this).val();
		});
		if (isEmptyYear || years === undefined || years.length == 0) {
			layer.msg('请填写年份');
			return false;
		}
		var resumes = [];
		var isEmptyResume = false;
		$('.layui-form input[id="resume"]').each(function (i) {
			if ($(this).val() === undefined || $(this).val() == '') {
				isEmptyResume = true;
			}
			resumes[i] = $(this).val();
		});
		if (isEmptyResume || resumes === undefined || resumes.length == 0) {
			layer.msg('请填写奖惩信息');
			return false;
		}
		if (years.length != resumes.length) {
			layer.msg('年份必需和奖惩信息对应');
			return false;
		}
		var kid = $('#kid').val();
		$.ajax({
			url: '/admin/scholar/rewardAndPunishSave?kid=' + kid + '&year=' + years + '&resume=' + resumes,
			type: 'POST',
			dataType: 'json',
			success: function(result) {
				if (result.code === 1) {
					layer.msg(result.msg);
					location.url = '/admin/scholar/toRewardAndPunish?kid=' + kid
				}
			}
		});
	}
</script>
{/block}